home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JCheckBox.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.0 KB  |  240 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JCheckBox.java    1.45 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18.  
  19. import javax.swing.plaf.*;
  20. import javax.accessibility.*;
  21.  
  22. import java.io.ObjectOutputStream;
  23. import java.io.ObjectInputStream;
  24. import java.io.IOException;
  25.  
  26.  
  27. /**
  28.  * An implementation of a CheckBox -- an item that can be selected or
  29.  * deselected, and which displays its state to the user. In a group
  30.  * of checkboxes, multiple checkboxes can be selected. 
  31.  *
  32.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/checkbox.html">How to Use CheckBoxes</a>
  33.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  34.  * for further documentation.
  35.  * <p>
  36.  * For the keyboard keys used by this component in the standard Look and
  37.  * Feel (L&F) renditions, see the
  38.  * <a href="doc-files/Key-Index.html#JCheckBox">JCheckBox</a> key assignments.
  39.  * <p>
  40.  * <strong>Warning:</strong>
  41.  * Serialized objects of this class will not be compatible with 
  42.  * future Swing releases.  The current serialization support is appropriate
  43.  * for short term storage or RMI between applications running the same
  44.  * version of Swing.  A future release of Swing will provide support for
  45.  * long term persistence.
  46.  *
  47.  * @see JRadioButton
  48.  *
  49.  * @beaninfo
  50.  *   attribute: isContainer false
  51.  *
  52.  * @version 1.45 08/28/98
  53.  * @author Jeff Dinkins
  54.  */
  55. public class JCheckBox extends JToggleButton implements Accessible {
  56.  
  57.     /**
  58.      * @see #getUIClassID
  59.      * @see #readObject
  60.      */
  61.     private static final String uiClassID = "CheckBoxUI";
  62.  
  63.  
  64.     /**
  65.      * Creates an initially unselected checkbox button with no text, no icon.
  66.      */
  67.     public JCheckBox () {
  68.         this(null, null, false);
  69.     }
  70.  
  71.     /**
  72.      * Creates an initially unselected checkbox with an icon.
  73.      *
  74.      * @param icon  the Icon image to display
  75.      */
  76.     public JCheckBox(Icon icon) {
  77.         this(null, icon, false);
  78.     }
  79.     
  80.     /**
  81.      * Creates a checkbox with an icon and specifies whether
  82.      * or not it is initially selected.
  83.      *
  84.      * @param icon  the Icon image to display
  85.      * @param selected a boolean value indicating the initial selection
  86.      *        state. If <code>true</code> the checkbox is selected
  87.      */
  88.     public JCheckBox(Icon icon, boolean selected) {
  89.         this(null, icon, selected);
  90.     }
  91.     
  92.     /**
  93.      * Creates an initially unselected checkbox with text.
  94.      *
  95.      * @param text the text of the checkbox.
  96.      */
  97.     public JCheckBox (String text) {
  98.         this(text, null, false);
  99.     }
  100.  
  101.     /**
  102.      * Creates a checkbox with text and specifies whether 
  103.      * or not it is initially selected.
  104.      *
  105.      * @param text the text of the checkbox.
  106.      * @param selected a boolean value indicating the initial selection
  107.      *        state. If <code>true</code> the checkbox is selected
  108.      */
  109.     public JCheckBox (String text, boolean selected) {
  110.         this(text, null, selected);
  111.     }
  112.  
  113.     /**
  114.      * Creates an initially unselected checkbox with 
  115.      * the specified text and icon.
  116.      *
  117.      * @param text the text of the checkbox.
  118.      * @param icon  the Icon image to display
  119.      */
  120.     public JCheckBox(String text, Icon icon) {
  121.         this(text, icon, false);
  122.     }
  123.  
  124.     /**
  125.      * Creates a checkbox with text and icon,
  126.      * and specifies whether or not it is initially selected.
  127.      *
  128.      * @param text the text of the checkbox.
  129.      * @param icon  the Icon image to display
  130.      * @param selected a boolean value indicating the initial selection
  131.      *        state. If <code>true</code> the checkbox is selected
  132.      */
  133.     public JCheckBox (String text, Icon icon, boolean selected) {
  134.         super(text, icon, selected);
  135.         setBorderPainted(false);
  136.         setHorizontalAlignment(LEFT);
  137.     }
  138.  
  139.     /**
  140.      * Notification from the UIFactory that the L&F
  141.      * has changed. 
  142.      *
  143.      * @see JComponent#updateUI
  144.      */
  145.     public void updateUI() {
  146.         setUI((ButtonUI)UIManager.getUI(this));
  147.     }
  148.  
  149.  
  150.     /**
  151.      * Returns a string that specifies the name of the L&F class
  152.      * that renders this component.
  153.      *
  154.      * @return "CheckBoxUI"
  155.      * @see JComponent#getUIClassID
  156.      * @see UIDefaults#getUI
  157.      * @beaninfo
  158.      *        expert: true
  159.      *   description: A string that specifies the name of the L&F class
  160.      */
  161.     public String getUIClassID() {
  162.         return uiClassID;
  163.     }
  164.  
  165.  
  166.     /**
  167.      * See JComponent.readObject() for information about serialization
  168.      * in Swing.
  169.      */
  170.     private void readObject(ObjectInputStream s) 
  171.     throws IOException, ClassNotFoundException 
  172.     {
  173.         s.defaultReadObject();
  174.     if (getUIClassID().equals(uiClassID)) {
  175.         updateUI();
  176.     }
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Returns a string representation of this JCheckBox. This method 
  182.      * is intended to be used only for debugging purposes, and the 
  183.      * content and format of the returned string may vary between      
  184.      * implementations. The returned string may be empty but may not 
  185.      * be <code>null</code>.
  186.      * <P>
  187.      * Overriding paramString() to provide information about the
  188.      * specific new aspects of the JFC components.
  189.      * 
  190.      * @return  a string representation of this JCheckBox.
  191.      */
  192.     protected String paramString() {
  193.     return super.paramString();
  194.     }
  195.  
  196. /////////////////
  197. // Accessibility support
  198. ////////////////
  199.  
  200.     /**
  201.      * Get the AccessibleContext associated with this JComponent
  202.      *
  203.      * @return the AccessibleContext of this JComponent
  204.      * @beaninfo
  205.      *       expert: true
  206.      *  description: The AccessibleContext associated with this CheckBox.
  207.      */
  208.     public AccessibleContext getAccessibleContext() {
  209.         if (accessibleContext == null) {
  210.             accessibleContext = new AccessibleJCheckBox();
  211.         }
  212.         return accessibleContext;
  213.     }
  214.  
  215.     /**
  216.      * The class used to obtain the accessible role for this object.
  217.      * <p>
  218.      * <strong>Warning:</strong>
  219.      * Serialized objects of this class will not be compatible with
  220.      * future Swing releases.  The current serialization support is appropriate
  221.      * for short term storage or RMI between applications running the same
  222.      * version of Swing.  A future release of Swing will provide support for
  223.      * long term persistence.
  224.      */
  225.     protected class AccessibleJCheckBox extends AccessibleJToggleButton {
  226.  
  227.         /**
  228.          * Get the role of this object.
  229.          *
  230.          * @return an instance of AccessibleRole describing the role of the object
  231.          * @see AccessibleRole
  232.          */
  233.         public AccessibleRole getAccessibleRole() {
  234.             return AccessibleRole.CHECK_BOX;
  235.         }
  236.  
  237.     } // inner class AccessibleJCheckBox
  238. }
  239.   
  240.